from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from .ser import *
from rest_framework.response import Response
class DianjiaAPIView(APIView):
    def get(self,request):
        data = Dianjia.objects.all()
        ser = DianjiaSer(data,many=True)
        return Response(ser.data)
    def post(self,request):
        data = request.data
        ser = DianjiaSer(data=data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response(ser.data)
class ShangpingAPIView(APIView):
    def get(self,request):
        cate_id = request.GET.get('cate_id')
        if cate_id:
            page = Shangping.objects.filter(cate_id=cate_id)
        else:
            page = Shangping.objects.all()
            ser = ShangpingSer(page,many=True)
            return Response(ser.data)
    def post(self,request):
        data = request.data
        ser = ShangpingSer(data=data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response(ser.data)
class ShangpingShanchu(APIView):
    def delete(self,request,pk):
        Shangping.objects.filter(id=pk).delete()
        return Response(status=204)


